package cn.fulong.smebj.admin.controller;

import cn.fulong.frame.config.Result;
import cn.fulong.smebj.entity.Maths;
import cn.fulong.smebj.mapper.InformationMapper;
import cn.fulong.smebj.mapper.MathsMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api("数学文化")
@RestController
@RequestMapping("/maths")
public class MathsController {
    @Autowired
    private MathsMapper mathsMapper;

    @Autowired
    private InformationMapper informationMapper;


    @ApiOperation("管理列表")
    @RequestMapping(value = "/list")
    public Result<Object> list(Integer pageNo, Integer pageSize) {
        IPage<Map<String,Object>> iPage = new Page<>(Optional.ofNullable(pageNo).orElse(0), pageSize);
        IPage<Maths> result = mathsMapper.queryList(iPage);
        return Result.success(result);
    }


    @ApiOperation("保存数学")
    @PostMapping(value = "/save")
    public Result<Object> save(String title,String content) {
        Maths maths = new Maths();
        maths.setContent(content);
        maths.setTitle(title);
        maths.setCreateTime(new Date(System.currentTimeMillis()));
        mathsMapper.insert(maths);
        return Result.success();
    }

    @ApiOperation("更新数学")
    @PostMapping(value = "/update")
    public Result<Object> update(Long id,String title,String content) {
        Maths maths = mathsMapper.selectById(id);
        maths.setContent(content);
        maths.setTitle(title);
        maths.setCreateTime(new Date(System.currentTimeMillis()));
        mathsMapper.updateById(maths);
        return Result.success();
    }

    @ApiOperation("删除数学")
    @PostMapping(value = "/delete")
    public Result<Object> delete(Long id) {
        mathsMapper.deleteById(id);

        return Result.success();
    }

    @ApiOperation("资源搜索")
    @RequestMapping(value = "/search")
    public Result<Object> search(String title) {
        List<Map<String,Object>> result = mathsMapper.search(title);
        List<Map<String,Object>> result2 = informationMapper.search(title);
        result.addAll(result2);
        Map map = new HashMap();
        map.put("数学文化",result);
        map.put("竞赛资讯",result2);
        return Result.success(map);
    }


}
